﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    public class Arc : Curve
    {
        #region API 函数
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="arc"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveRelease(ref IntPtr arc);

        /// <summary>
        /// 创建一段圆弧
        /// </summary>
        /// <param name="arc"></param>
        /// <param name="center">圆弧中心</param>
        /// <param name="start">圆弧起点</param>
        /// <param name="end">圆弧终点</param>
        /// <param name="normal">法向</param>
        /// <param name="edgeCount">圆弧分段数</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveCreate(out IntPtr arc, Point3D center, Point3D start, Point3D end, Vector3D normal, long edgeCount);

        /// <summary>
        /// 获取圆弧半径
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetRadius(IntPtr arccurve, out double radius);

        /// <summary>
        /// 获取圆弧起点
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetStartPoint(IntPtr arccurve, ref Point3D point);

        /// <summary>
        /// 获取圆弧终点
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetEndPoint(IntPtr arccurve, ref Point3D point);

        /// <summary>
        /// 获取圆弧平面 X 轴
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="axis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetXAxis(IntPtr arccurve, ref Vector3D axis);

        /// <summary>
        /// 获取圆弧平面 Y 轴
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="axis"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetYAxis(IntPtr arccurve, ref Vector3D axis);

        /// <summary>
        /// 获取圆弧圆心
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetCenter(IntPtr arccurve, ref Point3D point);

        /// <summary>
        /// 获取圆弧法向
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="normal"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetNormal(IntPtr arccurve, ref Vector3D normal);

        /// <summary>
        /// 获取圆弧起点角度
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetStartAngle(IntPtr arccurve, out double angle);

        /// <summary>
        /// 获取圆弧终点角度
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetEndAngle(IntPtr arccurve, out double angle);

        /// <summary>
        /// 判断圆弧是否是一个完整的圆
        /// </summary>
        /// <param name="arccurve"></param>
        /// <param name="isCircle"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUArcCurveGetIsFullCircle(IntPtr arccurve, out bool isCircle);
        #endregion

        /// <summary>
        /// 是否是一个完整的圆
        /// </summary>
        public bool IsCircle
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetIsFullCircle(Id, out bool isCircle));
                return isCircle;
            }
        }

        /// <summary>
        /// 圆心
        /// </summary>
        public Point3D Center
        {
            get
            {
                Point3D c = new Point3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetCenter(Id, ref c));
                return c;
            }
        }

        /// <summary>
        /// 圆弧半径
        /// </summary>
        public double Radius
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetRadius(Id, out double radius));
                return radius;
            }
        }

        /// <summary>
        /// 起点
        /// </summary>
        public Point3D Start
        {
            get
            {
                Point3D s = new Point3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetStartPoint(Id, ref s));
                return s;
            }
        }

        /// <summary>
        /// 终点
        /// </summary>
        public Point3D End
        {
            get
            {
                Point3D e = new Point3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetEndPoint(Id, ref e));
                return e;
            }
        }

        /// <summary>
        /// 起始角度
        /// </summary>
        public double StartAngle
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetStartAngle(Id, out double angle));
                return angle;
            }
        }

        /// <summary>
        /// 终止角度
        /// </summary>
        public double EndAngle
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetEndAngle(Id, out double angle));
                return angle;
            }
        }

        /// <summary>
        /// X 轴
        /// </summary>
        public Vector3D XAxis
        {
            get
            {
                Vector3D x = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetXAxis(Id, ref x));
                return x;
            }
        }

        /// <summary>
        /// Y 轴
        /// </summary>
        public Vector3D YAxis
        {
            get
            {
                Vector3D y = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetYAxis(Id, ref y));
                return y;
            }
        }

        /// <summary>
        /// 法向
        /// </summary>
        public Vector3D Normal
        {
            get
            {
                Vector3D n = new Vector3D();
                SketchUpExceptionHelper.ThrowSUResult(SUArcCurveGetNormal(Id, ref n));
                return n;
            }
        }

        internal Arc(IntPtr id) : base(id)
        {
        }

        /// <summary>
        /// 创建一个圆弧
        /// </summary>
        /// <remarks>如果起点和终点相同，则创建一个完整的圆。</remarks>
        /// <param name="center">圆心</param>
        /// <param name="start">起点</param>
        /// <param name="end">终点</param>
        /// <param name="normal">法向</param>
        /// <param name="edgeCount">分段数</param>
        /// <returns>圆弧</returns>
        public static Arc Create(Point3D center, Point3D start, Point3D end, Vector3D normal, int edgeCount)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUArcCurveCreate(out IntPtr id, center, start, end, normal, edgeCount));
            return new Arc(id);
        }

        /// <summary>
        /// 创建一个圆弧
        /// </summary>
        /// <remarks>默认法向朝上，分段数为12</remarks>
        /// <param name="center">圆心</param>
        /// <param name="start">起点</param>
        /// <param name="end">终点</param>
        /// <returns>圆弧</returns>
        public static Arc Create(Point3D center, Point3D start, Point3D end)
        {
            return Create(center, start, end, Vector3D.UnitZ, 12);
        }

        /// <summary>
        /// 创建一个圆
        /// </summary>
        /// <param name="center">圆心</param>
        /// <param name="radius">半径</param>
        /// <param name="normal">法向</param>
        /// <param name="edgeCount">分段数</param>
        /// <returns></returns>
        public static Arc CreateCircle(Point3D center, double radius, Vector3D normal, int edgeCount)
        {
            normal.GetVerticalAxis(out Vector3D x, out _);
            Point3D start = center + x * radius;
            return Create(center, start, start, normal, edgeCount);
        }

        /// <summary>
        /// 创建一个圆
        /// </summary>
        /// <remarks>法向朝上，分段为24段</remarks>
        /// <param name="center"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public static Arc CreateCircle(Point3D center, double radius)
        {
            return CreateCircle(center, radius, Vector3D.UnitZ, 24);
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUArcCurveRelease(ref Id));
        }
    }
}